use diesel::prelude::*;

use crate::{database::schema, route};

#[derive(Queryable, Selectable)]
#[diesel(table_name = schema::menu)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Menu {
    pub id: i32,
    pub name: String,
    pub icon: String,
    pub link: String,
    pub visibility_level: i32,
}

#[derive(Insertable, AsChangeset)]
#[diesel(table_name = schema::menu)]
pub struct NewMenu<'a> {
    pub name: &'a str,
    pub icon: &'a str,
    pub link: &'a str,
    pub visibility_level: i32,
}

impl<'a> From<&'a route::form::menu::NewMenuForm> for NewMenu<'a> {
    fn from(menu: &'a route::form::menu::NewMenuForm) -> Self {
        NewMenu {
            name: &menu.name,
            icon: &menu.icon,
            link: &menu.link,
            visibility_level: menu.visibility_level,
        }
    }
}

impl<'a> From<&'a Menu> for NewMenu<'a> {
    fn from(menu: &'a Menu) -> Self {
        NewMenu {
            name: &menu.name,
            icon: &menu.icon,
            link: &menu.link,
            visibility_level: menu.visibility_level,
        }
    }
}
